package nktest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author ganeshT
 */
public class DictionaryIDTest {

    private static final String MODULE_ID = "KRA0030E";
    private static String[] arrDict = {"D01354"};
    private final static String connectionString = "jdbc:db2://10.1.3.151:50000/KAILAS:currentSchema=KAILAS;currentFunctionPath=KAILAS;"; // @jve:decl-index=0:
    //localhost
//    private final static String connectionString = "jdbc:db2://localhost:50000/KAILAS:currentSchema=KAILAS;currentFunctionPath=KAILAS;"; // @jve:decl-index=0:

    public static void main(String[] args) throws Exception {
        Class.forName("com.ibm.db2.jcc.DB2Driver");
        Connection conn = null;
        PreparedStatement pstmt = null;
        PreparedStatement pgfmitSearch = null;
        PreparedStatement fmimstSearch = null;
        ResultSet rset = null;
        ResultSet pgmfitRset = null;
        ResultSet fmimstRset = null;
        boolean found = false;
        conn = DriverManager.getConnection(connectionString, "db2admin", "db2admin");
        pstmt = conn
                .prepareStatement(" select    FMFIID,PFPGID ,PFFMCD  from FMIMST F, PGMFIT PF    where F.FMFIID = PF.PFFMCD and F.FMDLTM = ' ' and PF.PFDLTM = ' ' and PF.PFPGID = ? and f.fmfiid = ?");
        pstmt.setString(1, MODULE_ID);
        for (int i = 0; i < arrDict.length; i++) {
            System.out.println("Checking " + arrDict[i]);
            pstmt.setString(2, arrDict[i]);
            rset = pstmt.executeQuery();
            if (rset != null) {
                found = false; // Resetting the found variable.
                while (rset.next()) {
                    found = true;
                }
            }
            // try to find the table ( PGMFIT or FMIMST )in which data is
            // not registered
            if (!found) {
                System.out.println("Dictionary ID not found : -> " + arrDict[i]);
                fmimstSearch = conn.prepareStatement("select FMFIID from FMIMST f where f.FMFIID = ?");
                fmimstSearch.setString(1, arrDict[i]);
                fmimstRset = fmimstSearch.executeQuery();
                if (fmimstRset != null) {
                    if (!fmimstRset.next()) {
                        System.out.println(arrDict[i] + "  Not found in FMIMST");
                    } else {
                        System.out.println(arrDict[i] + " Found in FMIMST");
                    }
                }
                pgfmitSearch = conn.prepareStatement("select PFPGID from PGMFIT p where p.PFPGID = ? and p.PFFMCD = ?");
                pgfmitSearch.setString(1, MODULE_ID);
                pgfmitSearch.setString(2, arrDict[i]);
                pgmfitRset = pgfmitSearch.executeQuery();
                if (pgmfitRset != null) {
                    if (!pgmfitRset.next()) {
                        System.out.println(arrDict[i] + "  Not found in PGMFIT");

                    } else {
                        System.out.println(arrDict[i] + "  Found in PGMFIT");
                    }
                }
            }
        }

    }
}
